package com.andnnl.design.chain;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenss on 2021/8/26.
 */
public class BillTest {
    public static void main(String[] args) {

        long curPage=1;
        long totalPage=6;

        int p=(int) (curPage*100/totalPage);
        System.out.println(p);
        if(1==1)return;

        List<VideoJieti> list = new ArrayList<>();
        list.add(new VideoJieti(1, "099", 1, 5000, new BigDecimal("22.00")));
        list.add(new VideoJieti(2, "099", 2, 10000, new BigDecimal("21.00")));
        list.add(new VideoJieti(3, "099", 3, 16000, new BigDecimal("20.00")));
        list.add(new VideoJieti(4, "099", 4, 34000, new BigDecimal("19.00")));
        list.add(new VideoJieti(5, "099", 5, -1, new BigDecimal("18.00")));
        list.forEach(b->{
            System.out.println(b.getId());
            if(b.getId()==2){
                System.out.println(b.getId());
                return;
            }
        });
        BillProcessor root = null;
        BillProcessor last = null;
        for (VideoJieti jieti : list) {
            BillProcessor next = new BillProcessor(jieti);
            if (root == null) {
                root = next;
            }else {
                last.setNextProcessor(next);
            }
            last = next;
        }
//        list.stream().reduce
        BigDecimal rs = null;
        rs = root.processData(BigDecimal.valueOf(0), 18000);
        System.out.println(rs);
        rs = root.processData(BigDecimal.valueOf(0), 36000);
        System.out.println(rs);
        rs = root.processData(BigDecimal.valueOf(0), 4000);
        System.out.println(rs);
        rs = root.processData(BigDecimal.valueOf(0), 5000);
        System.out.println(rs);
        rs = root.processData(BigDecimal.valueOf(0), 5001);
        System.out.println(rs);
        rs = root.processData(BigDecimal.valueOf(0), 0);
        System.out.println(rs);

        System.out.println(Integer.valueOf(1)==Integer.valueOf(1));
        System.out.println(Integer.valueOf(221)==Integer.valueOf(221));
    }
}

abstract class Processor {
    protected Processor next;
    protected VideoJieti jieti;
    protected BigDecimal bd10 = BigDecimal.valueOf(10);
    protected int count=0;//当前一级的条数
    protected BigDecimal currentBillNum;//当前阶梯价格

    public void setNextProcessor(Processor p) {
        next = p;
    }
    abstract protected BigDecimal processData(BigDecimal billNum, int data);
}

class BillProcessor extends Processor {
    public BillProcessor(VideoJieti jieti) {
        this.jieti = jieti;
        this.count=jieti.getCount();
    }
    @Override
    public void setNextProcessor(Processor p) {
        super.setNextProcessor(p);
        if (next.jieti.getCount() != -1) {
            next.count=next.jieti.getCount() - this.jieti.getCount();//设置成每一层过滤的条数
        }
        currentBillNum=BigDecimal.valueOf(count).multiply(jieti.getPrice()).divide(bd10);//预先计算当前阶梯价格
    }

    @Override
    protected BigDecimal processData(BigDecimal billNum, int data) {
        if (data <= count || count == -1) {//如果数量小于等于当前阶梯或者最后一级时，结束计算
            billNum = billNum.add(BigDecimal.valueOf(data).multiply(jieti.getPrice()).divide(bd10));//当前阶梯计算
            return billNum;
        } else {
            billNum = billNum.add(currentBillNum);//当前阶梯计算
            return next.processData(billNum, data - count);//关掉当前一级的数量，剩下的调用下一级
        }
    }
}

